package com.htht.job.executor.service.registry;

import com.htht.job.executor.dao.registry.RegistryDao;
import com.htht.job.executor.model.registry.Registry;
import com.htht.job.executor.util.specification.SimpleSpecificationBuilder;
import org.jeesys.common.jpa.dao.string.BaseDao;
import org.jeesys.common.jpa.service.string.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Created by zzj on 2018/1/30.
 */
@Transactional
@Service("registryService")
public class RegistryService extends BaseService<Registry>{
    @Autowired
    private RegistryDao registryDao;
    @Override
    public BaseDao<Registry> getBaseDao() {
        return registryDao;
    }
    @Value("${mqDestination}")
    String mqDestination;

    public void saveOrUpdate(Registry registry){
        Registry yRegistry=this.findByWhere(registry.getRegistryKey(),registry.getRegistryIp(),registry.getPort());
        if(yRegistry==null){
            registry.setCreateTime(new Date());
            registryDao.save(registry);
        }else{
            yRegistry.setMqConcurrency(registry.getMqConcurrency());
            yRegistry.setMqDestination(registry.getMqDestination());
            yRegistry.setUpdateTime(new Date());
            registryDao.save(yRegistry);
        }
    }
    public Registry findByWhere(String registryKey,String registryIp,String port){
        SimpleSpecificationBuilder<Registry> specification=new SimpleSpecificationBuilder();
        specification.add("registryKey","eq",registryKey);
        specification.add("registryIp","eq",registryIp);
        specification.add("port","eq",port);
        Registry registry=super.getBySpecification(specification.generateSpecification());
        return registry;
    }
    public Registry findByMqDestination(String mqDestination){
        return  registryDao.findByMqDestination(mqDestination);
    }
    public String getMqDestination(){
        return mqDestination;
    }
    public List<Registry> findAll(){
        return registryDao.findAll();
    }

}
